<template>
    <div>
        <!--登录表单
            :rules:表单校验规则
            ref:表单校验别名
            :model:绑定表单模型
        -->
        <el-form
                :rules="rules"
                ref="loginForm"
                :model="loginForm"
                v-loading="loading"
                element-loading-text="正在登录..."
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"
                class="loginContainer">
            <h3 class="loginTitle">系统登录</h3>
            <!--
                prop:表单校验属性字段
                v-model:组件绑定表单属性名
                auto-complete:是否开启自动补全
                placeholder:表单元素未输入提示内容
            -->
            <el-form-item prop="username">
                <el-input size="normal" type="text" v-model="loginForm.username" auto-complete="off"
                          placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input size="normal" type="password" v-model="loginForm.password" auto-complete="off"
                          placeholder="请输入密码" @keydown.enter.native="submitLogin"></el-input>
            </el-form-item>
            <!--
                @click:表单提交单击事件
            -->
            <el-button size="normal" type="primary" style="width: 100%;" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>



export default {
        name: "Login",
        data() {
            return {
                loading:false,//加载进度条
                //配置登录表单属性值
                loginForm: {
                    username: 'admin',
                    password: '123'
                },
                //配置校验规则(required:是否必填,message:校验未通过提示信息,trigger:触发校验规则事件)
                rules: {
                    username: [{required: true, message: '请输入用户名', trigger: 'blur'}],
                    password: [{required: true, message: '请输入密码', trigger: 'blur'}]
                }
            }
        },
        //配置表单事件触发方法
        methods: {
            //表单登录提交事件($refs.loginForm:通过$refs.ref属性值获取需校验表单)
            submitLogin() {
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading=true;//表单通过校验，进度条显示
                        //以组件方式提交登录表单数据
                      this.postKeyValueRequest("/doLogin",this.loginForm).then(resp=>{
                          this.loading=false;//请求发送成功后，隐藏进度条
                           if(resp)
                           {

                              //this.$message.success(JSON.stringify(resp.obj));
                                window.sessionStorage.setItem("user",JSON.stringify(resp.obj));//将登录用户存储在sessionStorage中
                                let targetPath=this.$route.query.redirect;//匿名登录请求页面路径
                                this.$router.replace((targetPath=='/'||targetPath==undefined)?'/home':targetPath);//登录表单正常登录后转发至home,匿名登录转发至登录后的目标页面
                           }
                       })
                    } else {
                        this.$message.error('表单校验未通过');
                        return false;
                    }
                });
            }
        }
    }
</script>

<style>
    .loginContainer {
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #fff;
        border: 1px solid #eaeaea;
        box-shadow: 0 0 25px #cac6c6;
    }

    .loginTitle {
        margin: 15px auto 20px auto;
        text-align: center;
        color: #505458;
    }

    .loginRemember {
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
</style>